package de.koller.million.grid.frame.plyManag;

import de.koller.million.grid.GNode;
import de.koller.million.grid.commands.RemoveGNode;
import de.koller.aframe.util.thread.ICommand;
import javax.swing.JOptionPane;

public class PlayerManagerFrame extends GNode {
	
	private final PlayerList list = new PlayerList( this );
	private final PlayerManagerFrameComp comp = new PlayerManagerFrameComp( this );
	
	@Override
	protected void init() {
		super.init();
		
		comp.setVisible( true );
		groot.frame.comp.pmButton.setEnabled( false );
		
		list.init();
		
		comp.pack();
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		list.notifyUpdate();
		
		int c = groot.engine.playerContainer.getPlayerInGrpG();
		if( c == 0 )
			comp.playerInGrp.setText( "<html><b>Keine</b> Spieler in der Gruppe</html>" );
		else
			comp.playerInGrp.setText( "<html><b>" + c + "</b> Spieler in Gruppe</html>" );
	}
	
	@Override
	protected void release() {
		super.release();
		
		comp.dispose();
		groot.frame.comp.pmButton.setEnabled( true );
	}
	
	public void closeThis() {
		groot.call( new RemoveGNode( this ) );
	}
	
	public void createPlayer( final String name ) {
		groot.call( new ICommand() {
			@Override public void handle( Object... params ) {
				if( name.isEmpty() )
					JOptionPane.showMessageDialog( null, "Ungültiger Name!", "Spieler erstellen", JOptionPane.ERROR_MESSAGE );
				else if( !groot.engine.playerContainer.isNameAvailable( name ) )
					JOptionPane.showMessageDialog( null, "Name bereits belegt!", "Spieler erstellen", JOptionPane.ERROR_MESSAGE );
				else
					groot.engine.playerContainer.createPlayer( name );
			}
		} );
	}
	
	public PlayerList newPlayerList() {
		return list;
	}
	
}
